﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPlatePrintSys.Domain.Events;
using CPlatePrintSys.Domain.Models;
using CPlatePrintSys.Win.ViewModels;
using CPlatePrintSys.Win.Controls;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.IO;
using System.Diagnostics;

namespace CPlatePrintSys.Win.Views
{
    /// <summary>
    /// ProcessSequenceView.xaml 的交互逻辑
    /// </summary>
    public partial class ProcessSequenceView : UserControl
    {
        private readonly IEventAggregator _eventAggregator;

        public ProcessSequenceView()
        {
            InitializeComponent();
            _eventAggregator = Prism.Ioc.ContainerLocator.Container.Resolve<IEventAggregator>();
        }

        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var grid = sender as DataGrid;

            if (grid?.SelectedItem is MergedPlateAnnotationInfoViewModel mergedViewModel)
            {
                var info = mergedViewModel.GetPrimaryOriginalData();
                _eventAggregator.GetEvent<EditPlateAnnotationEvent>().Publish(info);
            }
        }

        private void DataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var grid = sender as DataGrid;

            if (grid?.SelectedItem is MergedPlateAnnotationInfoViewModel mergedViewModel)
            {
                // 切换选择状态
                mergedViewModel.IsSelected = !mergedViewModel.IsSelected;
                
                // 确保DataGrid正确显示选择状态
                grid.SelectedItem = mergedViewModel;
                
                // 触发命令状态更新
                if (DataContext is ProcessSequenceViewModel viewModel)
                {
                    viewModel.AnnotationCommand?.RaiseCanExecuteChanged();
                    viewModel.ExportDocumentsCommand?.RaiseCanExecuteChanged();
                    viewModel.MergeLayoutCommand?.RaiseCanExecuteChanged();
                }
            }
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // 当DataGrid选择项改变时，触发命令状态更新
            if (DataContext is ProcessSequenceViewModel viewModel)
            {
                // 更新当前选中的合并项
                if (sender is DataGrid grid && grid.SelectedItem is MergedPlateAnnotationInfoViewModel selectedItem)
                {
                    viewModel.CurrentSelectedMergedItem = selectedItem;
                }
                
                // 触发命令状态更新
                viewModel.AnnotationCommand?.RaiseCanExecuteChanged();
                viewModel.ExportDocumentsCommand?.RaiseCanExecuteChanged();
                viewModel.MergeLayoutCommand?.RaiseCanExecuteChanged();
            }
        }

        private void WorkspaceTree_NodeSelected(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is BaseInfoTreeNode selectedNode && DataContext is ProcessSequenceViewModel viewModel)
            {
                viewModel.OnWorkSpaceNodeSelected(selectedNode);
            }
        }

        private void AnnotationPath_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is TextBlock textBlock && !string.IsNullOrEmpty(textBlock.Text))
            {
                var filePath = textBlock.Text;
                
                // 检查文件是否存在
                if (File.Exists(filePath))
                {
                    try
                    {
                        // 使用默认程序打开文件
                        Process.Start(new ProcessStartInfo
                        {
                            FileName = filePath,
                            UseShellExecute = true
                        });
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"无法打开文件: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    MessageBox.Show($"文件不存在: {filePath}", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
    }
}
